package com.af.monitorModule.util;

import com.af.monitorModule.timmer.RedisArrange;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.util.Random;

@Component
public class DayStatisticsImages {

    private static final JSONObject week = JSON.parseObject("{\"MONDAY\":\"一\",\"TUESDAY\":\"二\",\"WEDNESDAY\":\"三\",\"THURSDAY\":\"四\",\"FRIDAY\":\"五\",\"SATURDAY\":\"六\",\"SUNDAY\":\"日\"}");

    private static final Logger logger = LoggerFactory.getLogger(RedisArrange.class);

    public void createDayImages(JSONObject innerValue){
        BufferedImage bi = null;
        try {
            //文件形式无法直接读取，需要以流的形式去获取
            InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("dayImage/board-"+LocalDate.now().getDayOfWeek().getValue()+".png");
            assert inputStream != null;
            bi = ImageIO.read(inputStream);
        } catch (IOException e) {
            logger.error("获取背景图片失败---->" + e.getMessage());
            bi = new BufferedImage(1080, 1920, BufferedImage.TYPE_INT_RGB);
        }
        int width = bi.getWidth();
        int heigh = bi.getHeight();
        // 得到它的绘制环境(这张图片的笔)
        Graphics2D g2 = (Graphics2D) bi.getGraphics();
        g2.setColor(Color.white);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 抗锯齿

        // 写入报表图片的时间
        Font titleFont = new Font("黑体", Font.PLAIN, 35);
        g2.setFont(titleFont);
        String date ="今天是 " + LocalDate.now().toString() + "  星期" +week.getString(LocalDate.now().getDayOfWeek().name());
        g2.drawString(date, width - g2.getFontMetrics(titleFont).stringWidth(date) - 30, 50);

        //写入数据时间
        titleFont = new Font("宋体", Font.BOLD, 55);
        g2.setFont(titleFont);
        LocalDate yesterday= LocalDate.now().plusDays(-1);
        g2.drawString(yesterday.getMonthValue() + "月" + yesterday.getDayOfMonth() + "日", 50, 130);
        //写入の
        g2.drawString("の", 100, 210);
        //写入标题
        titleFont = new Font("宋体", Font.BOLD, 120);
        g2.setFont(titleFont);
        g2.drawString("运", 60, 340);
        g2.drawString("维", 60, 480);
        g2.drawString("日", 60, 620);
        g2.drawString("报", 60, 760);

        //画分割线
        g2.setStroke(new BasicStroke(5.0f));
        g2.drawLine(220, 270, 220, 830);

        //填写数据
        titleFont = new Font("宋体", Font.PLAIN, 45);
        g2.setFont(titleFont);
        g2.drawString("· 推送监控消息数量：" + innerValue.get("toDaySendMSG").toString() + " 条", 270, 340);
        g2.drawString("· 获取服务器性能指标：" + innerValue.get("toDayServerPush").toString() + " 例", 270, 450);
        g2.drawString("· 屏蔽夜间无用推送：" + innerValue.get("toDayMeaningless").toString() + " 次", 270, 560);
        g2.drawString("· 脚本自愈应用服务：" + innerValue.get("toDayServiceRestart").toString() + " 次", 270, 670);
        g2.drawString("· 阻断数据库死锁进程：" + innerValue.get("toDayKillDataBase").toString() + " 次", 270, 780);
        g2.drawString("· 定时任务执行", 270, 945);
        g2.drawString("成功：" + innerValue.get("成功").toString() + " 个", 700, 890);
        g2.drawString("失败：" + innerValue.get("失败").toString() + " 个", 700, 1000);
        // 释放对象
        g2.dispose();
        // 保存图片 JPEG表示保存格式
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("/oth/monitor/nginx/html/file/dayImages/" + yesterday.toString() + ".png");
            ImageIO.write(bi, "png", fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
            logger.info("生成昨日报表图片完成");
        } catch (IOException e) {
            logger.error("输出文件异常");
        }
    }
}
